<template>
  <div class="add-address-wrap">
    <van-nav-bar title="修改地址" left-arrow @click-left="onClickLeft" />
    <van-address-edit
      :area-list="areaList"
      :show-delete="isEdit"
      detail-rows="4"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      :address-info="AddressInfo"
    />
  </div>
</template>

<script>
import { getAddress, editAddress, deleteAddress } from "@/api/address.js";

import areaList from "assets/js/area.js";
import Vue from "vue";
import { NavBar } from "vant";
import { AddressEdit } from "vant";
import { Toast } from "vant";
import { Area } from "vant";

Vue.use(Area);
Vue.use(Toast);
Vue.use(AddressEdit);
Vue.use(NavBar);
export default {
  data() {
    return {
      areaList,
      isEdit: true,
      AddressInfo: {}
    };
  },
  methods: {
    // 初始数据
    async getData() {
      let rs = await getAddress(this.$route.query);
      // console.log(rs);
      this.AddressInfo = rs.data[0];
    },
    // 返回上一页
    onClickLeft() {
      this.$router.go(-1);
    },
    // 修改数据
    async onSave(content) {
      let rs = await editAddress(content);
      // console.log(rs)
      if (rs.status === 200) {
        Toast("修改成功");
        this.$router.go(-1);
      } else {
        Toast("修改失败请重试");
      }
    },
    // 删除数据
    async onDelete() {
      let rs = await deleteAddress(this.$route.query.id);
      if (rs.status === 200) {
        Toast("删除成功");
        this.$router.go(-1);
      } else {
        Toast("删除失败请重试");
      }
    }
  },
  created() {
    this.getData();
  }
};
</script>

<style lang="stylus" scoped>
.add-address-wrap {
  height: 100%;
  background-color: #f9f9f9;

  .van-nav-bar >>> .van-icon {
    color: #0f0f0f !important;
  }

  >>> .van-button--round {
    border-radius: 0px !important;
    border: none;
    outline: none;
  }

  >>> .van-button--danger {
    background-color: #c3a769 !important;
  }
}
</style>